<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" href="/resources/css/formSelects-v4.css"/>
    <link rel="stylesheet" href="/resources/layui/css/layui.css">
    <script rel="stylesheet" src="/resources/layui/layui.js"></script>
    <script rel="stylesheet" src="/resources/js/jquery.min.js"></script>
    <script src="/resources/js/function/Local.js"></script>
    <script src="/resources/js/function/Session.js"></script>
</head>
<body>

<table id="demo" lay-filter="test"></table>
</body>
</html>
<script>
    var table,form,layer,laytpl;
    function states(){
        //执行重载
        table.reload('demo', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                guanjian : $("#guanjian").val()
            }
        });
    }
    layui.config({
        base: '/resources/js/'
    }).extend({
        formSelects: 'formSelects-v4'
    });
    layui.use(['table','form','formSelects','laytpl','layer'], function(){
        table = layui.table;
        form=layui.form;
        layer=layui.layer;
        laytpl=layui.laytpl;
        //第一个实例
        table.render({
            elem: '#demo'
            ,url: new Local().base_url+'/user/show' //数据接口
            ,page: true //开启分页
            ,even : true
            ,toolbar:"#toolbarDemo"
            ,id : "demo"
            ,cols: [[ //表头
                {field:'id',type:'checkbox'}
                ,{field: 'id', title: 'ID'}
                ,{field: 'userCode', title: '用户账号'}
                ,{field: 'name', title: '用户别名'}
                ,{field: 'cjId', title: '创建人',templet:function(d){
                        var str=" - ";
                        if(d.chuangjian!=null){
                            str=d.chuangjian.name;
                        }
                        return str;
                }}
                ,{field: 'cjDate', title: '创建日期',templet:function(d){
                        var str=" - ";
                        if(d.cjDate!=null){
                            str=d.cjDate;
                        }
                        return str;
                }}
                ,{field: 'xgId', title: '修改人',templet:function(d){
                        var str=" - ";
                        if(d.xiugai!=null){
                            str=d.xiugai.name;
                        }
                        return str;
                }}
                ,{field:'xgDate',title:'修改日期',templet:function(d){
                        var str=" - ";
                        if(d.xgDate!=null){
                            str=d.xgDate;
                        }
                        return str;
                }}
                ,{field:'gongsiId',title:'所属公司',templet:function(d){
                        var str=" - ";
                        if(d.gongsi!=null){
                            str=d.gongsi.name;
                        }
                    return str;
                }}
                ,{field:'zhuangtai',title:'状态',templet:function(d){
                    var str="正常";
                    if(d.zhuangtai==0){
                        str="禁用";
                    }
                    return str;
                }}
                ,{title:'操作',width:"13%",toolbar: '#barDemo'}
            ]]
        });
        table.on('tool(test)',function (obj) {
            var da=JSON.parse(sessionStorage.getItem(new Session().user_login));
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            switch(layEvent){
                case "del":
                    layer.confirm('真的删除用户吗？', function(index){
                        layer.close(index);
                        var idstr="";
                        if(data.length<=0){
                            layer.msg("请选择后删除!");
                        }else {
                            var idstr="";
                            idstr += data.id + ",";
                            $.post(new Local().base_url + "/user/batchdel", {idstr: idstr}, function (da) {
                                layer.close(layer.index);
                                table.reload('demo');
                                layer.msg("删除用户成功!");
                                chongLog(data.id);
                            });
                        }
                    });
                    break;
                case "edit":
                    $.get("add.html",function(s){
                        layer.open({
                            type : 1,
                            title : '修改分区',
                            area:'70%',
                            content:s,
                            anim: 5,
                            btn : ['确定','取消'],
                            success:function(){
                                $("#xgId").val(da.id);
                                $("#name").val(data.name);
                                $("#userCode").val(data.userCode);
                                $("#yuan").show();
                                $("#yuanpwd").val(data.scal);
                                $("#id").val(data.id);
                                var t1 =setInterval(function () {
                                    console.log("等待中....");
                                    if($("#gongsiId option").length > 1){
                                        var $se=$("#gongsiId");
                                        $se.val(data.gongsiId);
                                        form.render();
                                        clearInterval(t1);
                                    }
                                },100);
                                var t2 =setInterval(function () {
                                    console.log("等待中....");
                                    if($("#roleId option").length > 1){
                                        var $se=$("#roleId");
                                        var role=data.role;
                                        if(role!=null){
                                            $se.val(role.id);
                                        }
                                        form.render();
                                        clearInterval(t2);
                                    }
                                },100);
                                $("#zhuangtai").attr("checked",data.zhuangtai==1);
                                form.render();
                            }
                            ,yes:function(){
                                var pwd=$("#password").val();
                                if(pwd==null||pwd==""){
                                    var yuanpwd=$("#yuanpwd").val();
                                    $("#password").val(yuanpwd);
                                }
                                var str = $("#form").serialize();
                                $.post(new Local().base_url+"/user/upUser",str,function(db) {
                                    layer.close(layer.index);
                                    table.reload('demo');
                                    layer.msg("修改用户成功!");
                                    chongLog(data.id);
                                });
                            }
                        });
                    });
                    break;
                case "zan":
                    var zhuangtai=1;
                    if(data.zhuangtai==zhuangtai){
                        layer.msg("已经可用了！");
                        return;
                    }
                    var id=data.id;
                    var beizhu=null;
                    $.post(new Local().base_url+"/user/setZhuangtai",{id:id,zhuangtai:zhuangtai,beizhu:beizhu,xgId:da.id},d=>{
                        table.reload('demo');
                        layer.msg("已恢复正常!");
                    });
                    break;
                case "cai":
                    var zhuangtai=0;
                    if(data.zhuangtai==zhuangtai){
                        layer.msg("已经禁用了！");
                        return;
                    }
                    var id=data.id;
                    var beizhu=null;
                    layer.prompt({title: '为何禁用？'}, function(value, index){
                        layer.close(index);
                        beizhu=value;
                        $.post(new Local().base_url+"/user/setZhuangtai",{id:id,zhuangtai:zhuangtai,beizhu:beizhu,xgId:da.id},d=>{
                            table.reload('demo');
                            layer.msg("已禁用管理员!");
                            chongLog(id);
                        });
                    });

                    break;
            }
        });
        table.on('toolbar(test)', function(obj) {
            switch (obj.event) {
                case "batchDelete":
                    layer.confirm('真的删除用户吗？', function(index){
                        var ids=[];
                        layer.close(index);
                        var idstr="";
                        var checkStatus = table.checkStatus('demo');
                        if(checkStatus.data.length<=0){
                            layer.msg("请选择后删除!");
                        }else {
                            for (let user of checkStatus.data) {
                                idstr += user.id + ",";
                                ids.push(user.id);
                            }
                            $.post(new Local().base_url + "/user/batchdel", {idstr: idstr}, function (da) {
                                layer.close(layer.index);
                                table.reload('demo');
                                layer.msg("删除用户成功!");
                                xhcongLog(ids);
                            });
                        }
                    });
                    break;
                case 'add':
                    $.get('add.html',function (s) {
                        //parent
                        layer.open({
                            type : 1,
                            title : '添加分区',
                            area:'70%',
                            content:s,
                            btn : ['确定','取消'],
                            success:function(){
                                var da=JSON.parse(sessionStorage.getItem(new Session().user_login));
                                $("#cjId").val(da.id);
                                form.render();
                            }
                            ,yes:function(){
                                var str = $("#form").serialize();
                                $.post(new Local().base_url+"/user/add",str,function(data){
                                    layer.close(layer.index);
                                    table.reload('demo');
                                    layer.msg("添加成功!");
                                });
                            }
                        });
                    });
                    break;
            }
        });

    });
</script>
<script>
    function chongLog(dataid){
        var da=JSON.parse(sessionStorage.getItem(new Session().user_login));
        if(da.id==dataid){
            sessionStorage.setItem(new Session().user_login,null);
            sessionStorage.setItem(new Session().user_perms,null);
            layer.msg("修改了本用户,请重新登录!",function(){
                top.location='/html/login/login.html';
            });
        }
    }
    function xhcongLog(shu){
        var da=JSON.parse(sessionStorage.getItem(new Session().user_login));
        for(var i=0;i<shu.length;i++){
            if(shu[i]==da.id){
                layer.msg("删除了本用户,请重新登录!",function(){
                    top.location='/html/login/login.html';
                });
                break;
            }
        }
    }
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDelete"><i class="layui-icon layui-icon-delete"></i> 批量删除</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a href="javascript:void(0)" lay-event="zan"><i class="layui-icon layui-icon-praise"></i></a>
    <a href="javascript:void(0)" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
    <a href="javascript:void(0)" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
    <a href="javascript:void(0)" lay-event="cai"><i class="layui-icon layui-icon-tread"></i></a>
</script>
